package com.frxuexi.user.view

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.room.AskLeave
import com.frxuexi.user.R
import com.google.gson.Gson
import com.tencent.mmkv.MMKV

/**
 * 员工查看自己的申请详情页面
 */
@Composable
fun UserAskDetailPage(navHostController: NavHostController, json: String) {
    val ask = Gson().fromJson(json, AskLeave::class.java)

    val kv = MMKV.defaultMMKV()
    val uname by remember {
        mutableStateOf(kv.decodeString("uname", "未登录").toString())
    }

    val role by remember {
        mutableStateOf(kv.decodeInt("role", 0))
    }

    Column {
        MyTitleBar(navHostController = navHostController, title = "请假详情")
        Spacer(modifier = Modifier.height(10.dp))
        Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
            Text(text = "申报人信息", color = Color.Blue)
            Card(
                colors = CardDefaults.cardColors(containerColor = Color.White),
                border = BorderStroke(1.dp, Color.LightGray)
            ) {
                Row(
                    Modifier
                        .fillMaxWidth()
                        .padding(20.dp)
                ) {
                    AsyncImage(
                        model = "https://img0.baidu.com/it/u=3465811579,2310782493&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1727629200&t=a4a813928f902aff74268863728d556b",
                        contentDescription = "",
                        modifier = Modifier
                            .size(80.dp)
                            .clip(CircleShape),
                        contentScale = ContentScale.Crop
                    )
                    Spacer(modifier = Modifier.width(10.dp))
                    Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceAround) {
                        Text(text = uname)
                        Text(text = if (role == 1) "经理" else if (role == 2) "员工" else "")
                    }
                }
            }
            Spacer(modifier = Modifier.height(20.dp))
            Text(text = "请假详情", color = Color.Blue)
            Text(text = "请假方式：${ask.type}")
            Text(text = "开始时间：${ask.start}")
            Text(text = "结束时间：${ask.end}")
            Text(text = "请假原因：${ask.reason}")

            Row(Modifier.padding(20.dp,10.dp)) {
                Column(horizontalAlignment = Alignment.CenterHorizontally) {

                    AsyncImage(
                        model = "https://img0.baidu.com/it/u=3465811579,2310782493&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1727629200&t=a4a813928f902aff74268863728d556b",
                        contentDescription = "",
                        modifier = Modifier
                            .size(40.dp)
                            .clip(CircleShape),
                        contentScale = ContentScale.Crop
                    )
                    Divider(
                        Modifier
                            .width(1.dp)
                            .height(30.dp))
                    val id=if(ask.status=="已提交") R.drawable.ic_action_commit else if(ask.status=="同意") R.drawable.ic_action_yes else R.drawable.ic_action_no
                    Image(painter = painterResource(id = id), contentDescription = "", modifier = Modifier
                        .size(40.dp))
                }
                Spacer(modifier = Modifier.width(8.dp))
                Column {
                    Text(text = "已提交")
                    Text(text = "申报时间：${ask.time}",color = Color.LightGray,
                        fontSize = 12.sp)
                    Spacer(modifier = Modifier.height(30.dp))
                    val status=if(ask.status=="已提交") "审批中" else ask.status
                    Text(text = status!!)
                    Text(
                        text = "审批时间：${ask.comTime}",
                        color = Color.LightGray,
                        fontSize = 12.sp
                    )
                }
            }
        }


    }
}